package metier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import webEquipeRechercheGwt.client.entityMirror.PersonMirror;
import webEquipeRechercheGwt.client.entityMirror.SoftwareMirror;

@Entity
@Table(name="SOFTWARE")
@NamedQueries(
		@NamedQuery(name="getSoftwares",
				query="select s from Software s " +
				"order by s.title")
			)
public class Software implements Serializable {

	private static final long serialVersionUID = 8348312263380108904L;
	
	private int id;
	private String website;
	private String title;
	private String description;
	private Collection<Person> developers;
	private SoftwareMirror loaded;
	
	// Accessors
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getWebsite() {
		return website;
	}
	public void setWebsite(String website) {
		this.website = website;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@ManyToMany(mappedBy="softwares")
	public Collection<Person> getDevelopers() {
		return developers;
	}
	public void setDevelopers(Collection<Person> developers) {
		this.developers = developers;
	}
	public void addDeveloper(Person developer){
		developers.add(developer);
	}
	public void fullLoad(){
		Iterator<Person> it = developers.iterator();
		while(it.hasNext()){
			Person p = it.next();
			p.toString();
		}
	}
	public SoftwareMirror getMirror() {
		if(loaded==null){
			loaded=new SoftwareMirror();
			loaded.setDescription(description);
			loaded.setId(id);
			loaded.setTitle(title);
			loaded.setWebsite(website);
			Collection<PersonMirror> developerMirrors = new ArrayList<PersonMirror>();
			for(Person p : developers){
				developerMirrors.add(p.getMirror());
			}
			loaded.setDevelopers(developerMirrors);
		}
		return loaded;
	}
	
}
